IF EXISTS (SELECT * FROM DBO.SYSOBJECTS WHERE NAME = 'SP_INSERTTHANNHAN')
BEGIN
	DROP PROC SP_INSERTTHANNHAN
END
GO
CREATE PROC SP_INSERTTHANNHAN
	@MANV		NVARCHAR(20),
	@TENTN		NVARCHAR(40),
	@PHAI		NVARCHAR(3),
	@NGAYSINH	DATETIME,
	@QUANHE		NVARCHAR(30), 
	@RESULT INT OUTPUT
AS
	BEGIN 
		IF(@MANV='' OR @TENTN='' OR @PHAI='' OR @NGAYSINH='' OR @QUANHE='')
		BEGIN
			SET @RESULT = 1;
		END
		ELSE
		BEGIN
			IF EXISTS (SELECT * FROM THANNHAN WHERE MA_NVIEN = @maNV AND TENTN = @tenTN)
			BEGIN
				SET @RESULT = 0
			END
			ELSE
			BEGIN
				INSERT INTO THANNHAN(MA_NVIEN,TENTN,PHAI,NGSINH,QUANHE)
				VALUES(@MANV,@TENTN,@PHAI,@NGAYSINH,@QUANHE)
				IF(@@ERROR<>0)
				BEGIN
					SET @RESULT = 1
					IF @@TRANCOUNT>0
					ROLLBACK TRAN
				END
				ELSE
				BEGIN
					SET @RESULT = 2
				END
			END
		END
	END
DECLARE @RESULT INT
EXEC SP_INSERTTHANNHAN '','CHAU LONG LONG Long','NU','07/23/2007','CON',@RESULT OUTPUT
PRINT @RESULT

